package com.jxl.controller;


import com.jxl.entity.po.Station;
import com.jxl.entity.query.StationPageQuery;
import com.jxl.entity.vo.PageResponse;
import com.jxl.entity.vo.ResponseResult;
import com.jxl.entity.vo.StationVO;
import com.jxl.service.IStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 站点表 前端控制器
 * </p>
 *
 * @author 金小梁
 * @since 2025-06-08
 */
@Api(tags = "站点管理")
@RestController
@RequestMapping("/station")
public class StationController {
    @Autowired
    private IStationService stationService;

    @ApiOperation(value = "分页查询站点信息")
    @PostMapping("/queryStations")
    public ResponseResult<PageResponse<StationVO>> queryStations(@RequestBody StationPageQuery stationPageQuery) {
        PageResponse<StationVO> stationVOList = stationService.queryStations(stationPageQuery);
        return ResponseResult.success(stationVOList);
    }

    @ApiOperation(value = "新增站点信息")
    @PostMapping("/addStation")
    public ResponseResult<String> addStation(@RequestBody Station station) {
        boolean save = stationService.save(station);
        if (!save) {
            throw new RuntimeException("站点编号重复");
        }
        return ResponseResult.success("新增站点成功");
    }


    @ApiOperation(value = "删除站点集合")
    @DeleteMapping("/deleteStation")
    public ResponseResult deleteStation(@RequestBody List<Integer> stationIds) {
        stationService.deleteByIds(stationIds);
        return ResponseResult.success();
    }


    @ApiOperation(value = "修改站点")
    @PutMapping("/editStation")
    public ResponseResult editStation(@RequestBody Station station) {
        boolean update = stationService.updateById(station);
        if (update) {
            return ResponseResult.success("修改站点成功");
        }
        return ResponseResult.error("修改站点失败");
    }


    @ApiOperation("查询所有的站点")
    @GetMapping("/queryAllStations")
    public ResponseResult queryAllStations() {
        List<Station> stations = stationService.list();
        return ResponseResult.success(stations);
    }

    @ApiOperation("根据线路id查询站点")
    @GetMapping("/queryStationsByRouteId/{id}")
    public ResponseResult<StationVO> queryStationsByRouteId(@PathVariable("id") Integer id) {
        List<StationVO> stationVOS = stationService.listById(id);
        return ResponseResult.success(stationVOS);
    }


}
